package org.example.xinguan;


import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.example.NumberInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class XinguanMapper extends Mapper<LongWritable, Text,Text, NumberInfo>{
    protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {
        //按行读取数据，并进行分割存储
        String line = value.toString();
        Iterable<String> split = Arrays.asList(line.split(","));
        ArrayList<String> datas = new ArrayList<>(10);

        //存储
        for (String s : split){
            datas.add(s);
        }
        if(datas.size()>9){ //防止字符越界
            //过滤，选取所需要的数据
            if ("China".equals(datas.get(1))||"Taiwan*".equals(datas.get(1))){
                String Province = "China".equals(datas.get(1))?datas.get(0) : "Taiwan";

                String date = datas.get(4);//日期
                Long Confirmed =Long.parseLong(datas.get(5)) ; //已确诊，累计
                Long Deaths = Long.parseLong(datas.get(6)); //死亡，累计
                Long Health = Long.parseLong(datas.get(7)); //恢复，累计
                //写入
                context.write(new Text(date),new NumberInfo(Confirmed,Deaths,Health,Province));
            }
        }



    }
}
